import Vue from 'vue'
import Router from 'vue-router'
import TabBar from '@/components/Tabbar.vue'
import Class from '@/components/Class.vue'
import Detail from '@/components/Detail.vue'
import Chart from '@/components/Chart.vue'
import Sign from '@/components/sign.vue'
import Addclass from '@/components/addclass.vue'
import Login from '@/components/login.vue'
import Register from '@/components/register.vue'
import chartDetail from '@/components/chartDetail.vue'
import config from '@/config'

Vue.use(Router)

 const router=new Router({
  routes: [
  { path: '/', component: TabBar,redirect:'/login',
     children: [
        {path:'/class',component: Class},
        {path:'/chart',component: Chart}
      ]      
  },
  { path: '/detail', component: Detail },
  { path: '/addclass', component: Addclass },
  { path: '/sign', component: Sign },
  { path: '/login', component: Login},
  { path: '/register', component: Register },
  { path: '/chartDetail', component: chartDetail }
]
})
 router.beforeEach((to, from, next) => {
  // 第一入口判断
  if (localStorage.user&&to.fullPath === '/login') {
      next('/class?id='+localStorage.user)
  }else if(!localStorage.user && to.fullPath !== '/login'&& to.path !=='/sign'){
      next('/login')
  }
  // 第二入口判断
  if (to.fullPath === '/chartDetail' && from.path === '/sign') {
    next('/chartDetail?id=2')
  }else if(from.path === '/chartDetail' && to.path === '/sign'){
    next('/chart')
  }

  // 签到判断
  if (to.path === '/chartDetail' && from.path === '/chart') {
    let signObj = config.getSignObj();
    if(signObj.hasOwnProperty('id' + to.query.id)) {
      next()
    } else {
      next('/sign?id='+to.query.id)
    }
  }else{
    next()
  }
  })
export default router
